﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib.CommonForm;

namespace AUI
{
    public partial class frmOrderTypesList : FormBase
    {
        int _OrderTypeId = 0;

        public int OrderTypeId
        {
            get { return _OrderTypeId; }
            set { _OrderTypeId = value; }
        }

        string _OrderTypeCode;

        public string OrderTypeCode
        {
            get { return _OrderTypeCode; }
            set { _OrderTypeCode = value; }
        }

        string _OrderTypeName;

        public string OrderTypeName
        {
            get { return _OrderTypeName; }
            set { _OrderTypeName = value; }
        }


        DataTable _dt = null;
        public frmOrderTypesList()
        {
            //Dành cho admin
            InitializeComponent();
            Initialize();
            this._isSelect = true;
            this.Text = "Chọn loại đơn hàng (Ctrl + Enter)";
            btnSelect.Visible = _isSelect;
            ShowLoadData();
            _IsLoading = false;
        }
        public frmOrderTypesList(string formText, bool isSelect, int OrderTypeId, string keyWord)
        {
            InitializeComponent();
            Initialize();
            if (formText != "")
            {
                this.Text = formText;
                this.TabText = formText;
            }
            this._isSelect = isSelect;
            this._keyWord = keyWord;
            this._OrderTypeId = OrderTypeId;
            btnSelect.Visible = _isSelect;
            if (isSelect)
            {
                this.Text = this.Text + " (Ctrl + Enter)";
            }

            ShowLoadData();
            _IsLoading = false;
        }
        public override void Show_Form()
        {
            this.Show();
        }
        void Initialize()
        {
            
            _listRole = tbl_RoleRight_Controller.Instance().GetInfo("LoaiDonHang");

            FormGlobals.dgv_SelectionMode_FullRowSelect(dgvList);
            FormGlobals.dgv_AutoSizeColumnsMode_BeginLoad(dgvList);
            dgvList.BorderStyle = BorderStyle.FixedSingle;

            FormGlobals.dgv_AddColumn_TextBox(dgvList, "OrderTypeCode", "Mã", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "OrderTypeName", "Tên", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "OrderTypeId", "OrderTypeId", "", 1, false, true);

            FormGlobals.dgv_ColumnsSortMode(dgvList, DataGridViewColumnSortMode.Programmatic);


            this.Activated += new EventHandler(frmUserList_Activated);
            dgvList.SelectionChanged += new EventHandler(dgvList_SelectionChanged);
        }

        void dgvList_SelectionChanged(object sender, EventArgs e)
        {
            if (_IsLoading) return;

            if (dgvList.SelectedRows.Count > 0)
            {
                _OrderTypeId = Convert.ToInt32(dgvList.SelectedRows[0].Cells["OrderTypeId"].Value);
                _OrderTypeCode = Convert.ToString(dgvList.SelectedRows[0].Cells["OrderTypeCode"].Value);
                _OrderTypeName = Convert.ToString(dgvList.SelectedRows[0].Cells["OrderTypeName"].Value);
            }
        }

        void frmUserList_Activated(object sender, EventArgs e)
        {
            if (_isSelect)
            {
                //txtNo.Focus();
            }
        }
        private void ShowLoadData()
        {
            _IsLoading = true;

            Splash.Show(LoadData, "");
            dgvList.DataSource = _dt;
            _itemCount = _dt.Rows.Count;
            if (_itemCount == 0)
            {
                lblTotalRows.Text = "  Không có dòng nào!  ";
            }
            else
            {
                lblTotalRows.Text = " Có " + _itemCount.ToString("#,#0") + " dòng ";
            }


            _IsLoading = false;
        }
        private void LoadData()
        {
            //Run in thread
            _dt = tbl_OrderTypes_Controller.Instance().GetAll();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                Form f = new frmOrderTypeInfo(FormGlobals.dgv_GetDataRow(dgvList));
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    ShowLoadData();
                    FormGlobals.dgv_ReSelection(dgvList, "OrderTypeId", _OrderTypeId);
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Form f = new frmOrderTypeInfo(null);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                _currentPage = 0;
                ShowLoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (FormGlobals.Message_Confirm(UIMessage.QUESTION_DELETE))
                {
                    if (tbl_OrderTypes_Controller.Instance().Delete(_OrderTypeId))
                    {
                        FormGlobals.Message_InTaskbar(UIMessage.DETETE_COMPLETE);
                        ShowLoadData();
                    }
                    else
                        FormGlobals.Message_Error(new Exception( UIMessage.DETETE_ERROR));
                }
            }
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                if (_isSelect)
                    btnSelect.PerformClick();
                else
                    if (btnEdit.Enabled)
                        btnEdit.PerformClick();
        }


        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (_currentPage > 0)
            {
                _currentPage = 0;
                ShowLoadData();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (_totalPage > 0 && _currentPage < _totalPage - 1)
            {
                _currentPage++;
                ShowLoadData();
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (_currentPage > 0)
            {
                _currentPage--;
                ShowLoadData();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (_totalPage > 0 && _currentPage < _totalPage - 1)
            {
                _currentPage = _totalPage - 1;
                ShowLoadData();
            }
        }

        void Refresh()
        {
            if (!_IsLoading)
            {
                _currentPage = 0;
                ShowLoadData();
            }
        }

        private void btnSearh_Click(object sender, EventArgs e)
        {
            Refresh();
        }



        private void frmUserList_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Down:
                    if (dgvList.Rows.Count > 0)
                    {
                        dgvList.Focus();
                    }
                    break;
                case Keys.Insert:
                case Keys.Control | Keys.Enter:
                    btnSelect.PerformClick();
                    break;
            }
        }



        private void dgvList_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dgv_ColumnHeaderMouseClick(sender, e, delegate { ShowLoadData(); });
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            if (!FormGlobals.Message_Confirm("Bạn có chắc chắn " + btnExcel.Text + "?")) return;
            try
            {
                int out1 = 0;
                DataTable dtExcel = tbl_OrderTypes_Controller.Instance().GetAll();
                ReadExcelFile.MSExcelExport(FormGlobals.dgv_GetColumnHeaderText(dgvList, dtExcel), this.Text);
            }
            catch (Exception ex)
            {
                FormGlobals.Message_Warning(ex.Message);
                return;
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                _dlgResult = DialogResult.OK;
                this.Close();
            }
        }

    }
}
